package com.wolfpire.workflow.spring.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wolfpire.system.common.EasyuiPage;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.base.dao.IBaseHibernateDao;
import com.wolfpire.system.common.base.service.impl.BaseHibernateService;
import com.wolfpire.workflow.dao.WfTypeDao;
import com.wolfpire.workflow.om.WfType;
import com.wolfpire.workflow.spring.WfTypeService;

@Transactional
@Service("wfTypeService")
public class WfTypeServiceImpl extends BaseHibernateService<WfType, Long> implements WfTypeService {

	@Resource private WfTypeDao wfTypeDao;

	@Override
	public List<WfType> list(WfType wfType) {
		return this.wfTypeDao.list(wfType);
	}

	@Override
	protected IBaseHibernateDao<WfType, Long> getEntityDao() {
		return this.wfTypeDao;
	}

	@Override
	public Page<WfType> setPageDataList(EasyuiPage<WfType> page, WfType filterWfType) {
		return this.wfTypeDao.findWfTypes(page, filterWfType);
	}

	@Override
	public void saveOrUpdate(WfType wfType) {
		Long parentId = wfType.getParentId();
		if (null != parentId) {
			//判断是否存在父组织，如果存在父组织，将其父组织设置为非叶子节点
			WfType parentWfType = this.wfTypeDao.get(parentId);
			parentWfType.setLeaf(false);
			this.wfTypeDao.save(parentWfType);
		}
		if (null != wfType.getId()) {
			List<WfType> children = this.findChildRen(wfType.getId());
			if (!CollectionUtils.isEmpty(children)) {
				//判断是否存在子节点，如果存在，将其设置为非叶子节点
				wfType.setLeaf(false);
			}
		}
	}

	private List<WfType> findChildRen(Long id) {
		WfType wfType = new WfType();
		wfType.setParentId(id);
		return this.wfTypeDao.list(wfType);
	}
	

}
